package com.sinosoft.perfect.system._.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.system._.dao.ILoginDao;
import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._.po.TUserImg;
import com.sinosoft.perfect.system._.util.SystemUtil;
import com.sinosoft.perfect.system._.vo.LoginUser;
import com.sinosoft.perfect.system._.vo.LoginWroksiteUnitValidate;

public class LoginServiceImpl implements ILoginService {

	private ILoginDao  iloginDao;

	public void setIloginDao(ILoginDao iloginDao) {
		this.iloginDao = iloginDao;
	}


	@Override
	public int validate(String username, String password,String loginIpAddress) throws NoSuchAlgorithmException, SQLException {
		TUser tuserInfo = this.makeMD5PasswordTUserInfo(username, password);
		int validateIntResult = 1;
		try {
			String  resultUserIdString = iloginDao.validateUsernamePassword(tuserInfo);
			if(resultUserIdString != null && !resultUserIdString.equals("null")){
				boolean validateSite = this.validateSite(resultUserIdString, loginIpAddress);
				if(!validateSite){
					validateIntResult = -1;
				}
			}else{
				validateIntResult = 0;
			}
		} catch (SQLException e) {
			throw e;
		}
		return validateIntResult;
	}

	private boolean validateSite(String userId,String loginIpAddress) throws SQLException{
		boolean result = false;
		List<LoginWroksiteUnitValidate> sites = iloginDao.queryValidateLoginWorkSiteIp(userId);
		if(sites.size()==0)result=true;		
		for (int i = 0; i < sites.size(); i++) {
			LoginWroksiteUnitValidate site = sites.get(i);
			String ip = site.getSiteIp();
			if(ip == null || loginIpAddress.equals(ip)){
				result = true;
				break;
			}
		}
		return result ;
	}	
	
	@Override
	public LoginUser queryUserInfoAndLog(String username,String loginIpAddress) throws SQLException {
		final LoginUser l = iloginDao.queryUserInfo(username);
		l.setTloginLog(iloginDao.insertUserLoginLog(loginIpAddress,l.getUserMainId()));
		l.setUnits(iloginDao.queryUserUnitsInfo(l.getUserMainId()));
		return l;
	}

	@Override
	public List<TRoleSystem> queryUserRoleSystemsInfo(String userId)
			throws SQLException {
		return iloginDao.queryUserRoleSystemsInfo(userId);
	}

	@Override
	public void logoutUpdateLogInfo(String logId) throws SQLException {
		iloginDao.logoutUpdateLogInfo(logId);
	}
	
	private TUser makeMD5PasswordTUserInfo(String username,String password) throws NoSuchAlgorithmException{
		String newMd5Password = SystemUtil.md5(password);
		TUser tuserInfo = new TUser();
		tuserInfo.setLoginCode(username);
		tuserInfo.setLoginName(username);
		tuserInfo.setLoginPassword(newMd5Password);
		return tuserInfo;
	}

	@Override
	public void registerLoginStatus(String loginId) throws SQLException {
		iloginDao.registerLoginStatus(loginId);
	}

	@Override
	public InputStream generateLoginImg(String userMainId) throws SQLException {
		final TUserImg tuserImg = iloginDao.selectLoginImgInfo(userMainId);
		if(tuserImg == null ||  tuserImg.getImg() == null){
			return null;
		}else{
			return new ByteArrayInputStream(tuserImg.getImg());
		}
	}
	
}
